
import { EventTouch, Label, Node, Sprite, Toggle, _decorator } from "cc";
import { BaseUI } from "../../framework/base/BaseUI";
import { AudioMgr } from "../../framework/manager/AudioMgr";
import { UIMgr } from "../../framework/manager/UIMgr";
import { CacheUtils } from "../../framework/utils/CacheUtils";
import { AudioId, UIID } from "../config/Config";
import { GameLogicMgr } from "../gameLogic/GameLogicMgr";
import { PlayerMgr } from "../manager/PlayerMgr";
const { ccclass, property } = _decorator;

@ccclass('UISetting')
export class UISetting extends BaseUI {

    /**底部节点 */
    @property(Node)
    downNode: Node = null;

    
    /**音乐 音效 震动*/
    @property(Node)
    toggle1NL: Node[] = [];



    /**动画节点 */
    @property(Node)
    aniN: Node = null;

    //是否正在游戏
    isGaming: boolean = false;


    callback: Function = null;
    // //退出回调
    // callback_backHome: Function = null;
    // //重置回调
    // callback_resetStart: Function = null;

    private isSetting: boolean = false
    private titleText: string = ""
    public init(param1?: any, param2?: any, param3?: any, param4?: any, param5?: any): void {
        this.isGaming = UIMgr.getInstance().getUIById(UIID.UIGame) != null// GameMgr.getInstance().getGameState()
        this.isSetting = param1 ? param1 : false
        this.titleText = param2 
        this.callback = param3
        this.findChilds("btnCloseView").active = this.isSetting
        this.findChilds("mid").active = true
        !this.isSetting&& GameLogicMgr.instance.pauseGame()
    }

    start() {
        this.initModule();
        this.initView();
    }

    initModule() {

    }
  


    initView() {
        this.downNode.active = this.isGaming
        this.findChilds("titleLab").getComponent(Label).string = this.titleText
        // this.dCluData.active = !this.isGaming&&PlayerMgr.instance.isCanSave&&!!PlayerMgr.instance.userCloudData

        this.toggle1NL[0].getComponent(Toggle).isChecked = CacheUtils.canPlayMusin()
        this.toggle1NL[0].getComponent(Sprite).enabled = !CacheUtils.canPlayMusin()

        this.toggle1NL[1].getComponent(Toggle).isChecked = CacheUtils.canPlayEffect()
        this.toggle1NL[1].getComponent(Sprite).enabled = !CacheUtils.canPlayEffect()

        this.toggle1NL[2].getComponent(Toggle).isChecked = PlayerMgr.getInstance().getUserData().getShark()
        this.toggle1NL[2].getComponent(Sprite).enabled = !PlayerMgr.getInstance().getUserData().getShark()
        this.findChilds("btnRecovery").onClick(this.onRecoveryGame, this)
        this.findChilds("backHome").onClick( this.clickHome, this)
    }
    onRecoveryGame() {
        this.closeUI();
        !this.isSetting&& GameLogicMgr.instance.resumeGame()
    }
    closeBtn() {
        this.closeUI();
        // UIMgr.getInstance().closeById(UIID.UIGame)
        !this.isSetting&& GameLogicMgr.instance.resumeGame()
    }
    onDownloadCloud(){
        this.callback&&this.callback(PlayerMgr.instance.userCloudData)
        this.closeUI()
    }

    clickHome() {
        this.closeUI();
        UIMgr.getInstance().openUI(UIID.UIExitGame,UIID.Default,true,"退出游戏")
    }
    clickToggle(node: EventTouch, args: number) {
        this.scheduleOnce(() => {
            if (this.node && this.node.isValid) {
                let check = this.toggle1NL[args - 1].getComponent(Toggle).isChecked
                this.toggle1NL[args - 1].getComponent(Sprite).enabled = !check
                if (args == 1) {
                    CacheUtils.setPlayMusic(check);
                    if (CacheUtils.canPlayMusin()) {
                        let curAudioId = this.isGaming ? AudioId.mainBgm : AudioId.mainBgm;
                        let curMusic = this.isGaming ? "MainBGM" : "MainBGM";
                        // if (this.isGaming) 
                        AudioMgr.getInstance().playMusic(curAudioId, curMusic);
                    }
                    else {
                        let curMusic = this.isGaming ? "MainBGM" : "MainBGM";
                        // if (this.isGaming)
                        AudioMgr.getInstance().stopMusic(curMusic);
                    }
                } else if (args == 2) {
                    CacheUtils.setPlayEffect(check);
                    if (CacheUtils.canPlayEffect()) {
                        // AudioMgr.getInstance().playEffect(AudioId.goldEffect);
                    }
                    else {
                        AudioMgr.getInstance().stopEffect();
                    }
                } else if (args == 3) {
                    PlayerMgr.getInstance().getUserData().setShark(!PlayerMgr.getInstance().getUserData().getShark());
                }
            }
        })
    }

    onCloseUI(uid: any, msg: string) {

    }

}
